package models

import (
	"gintest/database"
	"time"
)

type StoreUser struct{
	Id int `json:"id" gorm:"column:id;primary_key" form:"id"`
	StoreId int `json:"store_id" gorm:"column:store_id" form:"store_id"`
	UserName string `json:"username" gorm:"column:username" form:"username"`
	Phone  string `json:"phone" gorm:"column:phone" form:"phone"`
	PassWord  string `json:"password" gorm:"column:password" form:"password"`
	Name  string `json:"name" gorm:"column:name" form:"name"`
	CreateAt time.Time  `json:"create_at" gorm:"column:create_at;type:datetime" time_format:"2006-01-02 15:04:05" form:"create_at"`
	UpdateAt time.Time  `json:"update_at" gorm:"column:update_at;type:datetime" time_format:"2006-01-02 15:04:05" form:"update_at"`
	DeleteAt time.Time  `json:"delete_at" gorm:"column:delete_at;type:datetime" time_format:"2006-01-02 15:04:05" form:"delete_at"`

	StoreUserThird StoreUserThird `json:"storeuserthird" gorm:"ForeignKey:UserId;AssociationForeignKey:Id"`
}
func (StoreUser) TableName() string {
	return "store_users"
}

func GetStoreUserList(page uint64,pagesize uint64,keyword string,store_id uint64)(data []StoreUser,total uint64){
	var offset=(page-1)*pagesize
	db:=database.SqlDB.Model(StoreUser{})

	if len(keyword)!=0{
		db=db.Where("concat(id,username,phone,name) like ?","%"+keyword+"%")
	}
	if store_id!=0{
		db=db.Where("store_id = ?",store_id)
	}
	db=db.Where("delete_at = 0 or delete_at is null")

	db.Offset(offset).Limit(pagesize).Order("id desc").Find(&data)

	db.Model(StoreUser{}).Count(&total)
	return data,total
}

func AddStoreUser(data *StoreUser) error{
	if data.UpdateAt.IsZero(){
		data.UpdateAt=time.Now()
	}
	if data.CreateAt.IsZero(){
		data.CreateAt=time.Now()
	}
	db:=database.SqlDB.Model(StoreUser{})
	if data.StoreUserThird.ThirdKey==""{
		db=db.Omit("StoreUserThird")
	}
	return db.Create(data).Error
}
func DelStoreUserByIds(ids []int) error{
	//return database.SqlDB.Model(User{}).Where("id in (?)",ids).Delete(User{}).Error
	return database.SqlDB.Model(StoreUser{}).Where("id in (?) and (delete_at = 0 or delete_at is null)",ids).Update("delete_at",time.Now().Local().String()).Error
}
func GetStoreUserById(id int) (data StoreUser){
	database.SqlDB.Model(StoreUser{}).Where("id=?",id).First(&data);
	return data
}
func GetStoreUserByPhone(phone string)(data StoreUser){
	database.SqlDB.Model(StoreUser{}).Where("phone=?",phone).First(&data);
	return data
}
func EditStoreUserById(id int,data interface{}) error{
	//update_at不存在，填充当前时间
	if _, has := data.(map[string]interface{})["update_at"]; !has {
		data.(map[string]interface{})["update_at"]=time.Now()
	}
	return database.SqlDB.Model(&StoreUser{}).Where("id=?",id).Updates(data).Error
}
//验证手机号唯一性
func UniquePhone(not_id uint64,code string)(total int){
	return baseUnique(not_id,"phone",code)
}
//验证用户名唯一性
func UniqueUserName(not_id uint64,username string)(total int){
	return baseUnique(not_id,"username",username)
}
//字段唯一验证
func baseUnique(not_id uint64,fd_name,val string)(total int){
	db:=database.SqlDB.Model(StoreUser{})
	if not_id!=0{
		db=db.Where("id !=?",not_id)
	}
	sql:="(delete_at = 0 or delete_at is null) and (" + fd_name + " = ?)"
	db.Where(sql,val).Count(&total)
	return
}
func GetStoreUserByOpenId(openid string)(data StoreUser){
	database.SqlDB.Model(StoreUser{}).Select("store_users.*").Joins("left join store_users_third on store_users_third.user_id=store_users.id").Where("store_users_third.third_key=?",openid).First(&data);
	return data
}
